﻿using asprise_ocr_api;
using mshtml;
using Pay.Cache;
using Pay.Common;
using Pay.Configuration;
using Pay.Container;
using Pay.Model.Dto;
using Pay.Model.Model;
using Pay.ServiceImplement;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pay.Consoles
{
    class Program
    {

        static void Main(string[] args)
        {
            //依赖注入
            LocManager.Register();
            // 初始化配置
            Config.Init(ConfigurationManager.AppSettings["config:path"]);

            // 初始化redis连接
            RedisConnection.Init(Config.GetValue("redis:connection"), Config.GetValue("redis:key"));

            for (var i = 0; i < 100; i++)
            {
                try
                {
                    MainAsync(args).GetAwaiter().GetResult();
                }
                catch (Exception e)
                {

                }
            }

            Console.ReadKey();
        }

        static async Task MainAsync(string[] args)
        {
            var service = new PayService();

            var alipayModel = new AlipayModel();

            var input = new PayInput
            {
                // amount = new Random().Next(1, 100),
                amount = 1,
                mch_code = "10000",
                mch_trade_no = DateTime.Now.Ticks.ToString(),
                notify_url = "http://www.test.com/notify",
                param = "",
                pay_type = Model.Enums.PayType.Alipay,
                return_url = "http://www.test.com/return",
                timespan = DateTime.Now.ToUnixTimespan()
            };

            Dictionary<string, string> signData = new Dictionary<string, string>();
            foreach (PropertyInfo p in input.GetType().GetProperties())
            {
                if (p.GetValue(input) != null && !string.IsNullOrWhiteSpace(p.GetValue(input).ToString()) && p.Name.ToLower() != "sign")
                {
                    signData.Add(p.Name, p.GetValue(input).ToString());
                }
            }

            alipayModel.PushData(signData);

            input.sign = alipayModel.CreateSign("476A3AF7E9744E09B76A021E46035A58");

            await service.CreatePayOrder(input);
        }

        //void ImageCode()
        //{
        //    AspriseOCR.SetUp();
        //    AspriseOCR ocr = new AspriseOCR();
        //    ocr.StartEngine("eng", AspriseOCR.SPEED_FASTEST);
        //    string s = ocr.Recognize("D:\\1.png", 0, 0, 0, 0, 0, AspriseOCR.CONFIG_PROP_KEY_VALUE_SEPARATOR, AspriseOCR.OUTPUT_FORMAT_PLAINTEXT);

        //    ocr.StopEngine();
        //}
    }
}
